/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gebze.Util;

import java.io.*;

/**
 * Server tarafından her client için dinleme clası
 *
 * @author Özcan ÇAĞIRICI <ozcan.cagirici@gmail.com>
 *
 * @author Hatice Şahin <haticesahin.09@gmail.com>
 */
public class GbzScannerClientHandler extends Thread {

    private InputStream inputStream;
    private OutputStream out;
    private transient boolean connected = true;

    public GbzScannerClientHandler(InputStream ins, OutputStream outs) {
        inputStream = ins;
        out = outs;
    }

    @Override
    public void run() {
    }

    /**
     * client tarafından gelen mesajların okundugu method
     */
    public String getClientMessage() {
        BufferedReader buffReader = new BufferedReader(new InputStreamReader(inputStream));
        String clientMesaji = null;
        try {
            clientMesaji = buffReader.readLine();
        } catch (IOException ex) {
            System.out.println("Mesaj okunurken hata olustu: " + ex);
        }

        return clientMesaji;
    }

    /**
     * serverin istegini(mesajını) clienta gonderir.
     *
     * @param hedefIP
     * @param serverMesaj
     */
    public void sendMessageToClient(GbzScannerClientHandler clientHandler, String serverMesaj) {

        try {
            OutputStreamWriter osw = new OutputStreamWriter(clientHandler.getOut());
            osw.write(serverMesaj);
        } catch (IOException ex) {
            System.out.println("Mesaj gonderilemedi.. ");
        }
    }

    public InputStream getIn() {
        return inputStream;
    }

    public OutputStream getOut() {
        return out;
    }

    public boolean isConnected() {
        return connected;
    }

    public void setConnected(boolean connected) {
        this.connected = connected;
    }
}
